package com.gogant.spider.http;

import com.gogant.spider.core.Connector;
import com.gogant.spider.core.ConnectorFactory;
import com.gogant.spider.core.PickerConfigurer;
import com.gogant.spider.core.Spider;
import com.gogant.spider.core.SpiderException;
import java.net.HttpURLConnection;
import java.nio.charset.Charset;

/**
 * Http网页爬虫
 */
public class HttpSpider extends Spider<HttpRequest, HttpResponse, HttpURLConnection> {
    private Charset defaultContentCharset;

    public HttpSpider() {
        this(1);
    }

    public HttpSpider(int nThreads) {
        super(nThreads, new HttpConnectorFactory());
    }

    /**
     * 启用URL提取，并可根据返回的PickerConfigurer进行定制提取
     * @return
     */
    public PickerConfigurer enableURLPicker() {
        return enableURLPicker(new HttpURLPicker());
    }

    /**
     * 获取Http页面的默认编码
     * @return
     */
    public Charset getDefaultContentCharset() {
        return defaultContentCharset;
    }

    /**
     * 设置Http页面的默认编码
     * @param defaultContentCharset
     */
    public void setDefaultContentCharset(Charset defaultContentCharset) {
        this.defaultContentCharset = defaultContentCharset;
    }

    private static class HttpConnectorFactory implements ConnectorFactory<HttpSpider, HttpRequest, HttpResponse, HttpURLConnection> {
        public Connector<HttpSpider, HttpRequest, HttpResponse, HttpURLConnection> createConnector(String protocol) throws SpiderException {
            if ("http".equalsIgnoreCase(protocol) || "https".equalsIgnoreCase(protocol)) {
                return new HttpConnector();
            } else {
                throw new SpiderException("unsupported protocol: " + protocol);
            }
        }
    }
}
